﻿// 地图行数
var ROW_NUM = 40;
var COLUMN_NUM = 40;
var IMAGE_DIR = "css/images/tiles/";
var TILES = [ {
	type : 0,
	name : '路口中心',
	image : IMAGE_DIR + 'cntr.png'
}, {
	type : 1,
	name : '^v南北方向路面',
	image : IMAGE_DIR + 'snr.png'
}, {
	type : 2,
	name : '<>东西方向路面',
	image : IMAGE_DIR + 'ewr.png'
}, {
	type : 3,
	name : '>V东南拐弯',
	image : IMAGE_DIR + 'es.png'
}, {
	type : 4,
	name : 'V<南西拐弯',
	image : IMAGE_DIR + 'sw.png'
}, {
	type : 5,
	name : '<^西北拐弯',
	image : IMAGE_DIR + 'wn.png'
}, {
	type : 6,
	name : '^>北东拐弯',
	image : IMAGE_DIR + 'ne.png'
}, {
	type : 7,
	name : '|-东三岔路',
	image : IMAGE_DIR + 'e3.png'
}, {
	type : 8,
	name : '_|_北三岔路',
	image : IMAGE_DIR + 'n3.png'
}, {
	type : 9,
	name : 'T南三岔路',
	image : IMAGE_DIR + 's3.png'
}, {
	type : 10,
	name : '-|西三岔路',
	image : IMAGE_DIR + 'w3.png'
}, {
	type : 11,
	name : '东开始路口',
	image : IMAGE_DIR + 'e.png'
}, {
	type : 12,
	name : '南开始路口',
	image : IMAGE_DIR + 's.png'
}, {
	type : 13,
	name : '西开始路口',
	image : IMAGE_DIR + 'w.png'
}, {
	type : 14,
	name : '北开始路口',
	image : IMAGE_DIR + 'n.png'
}, {
	type : 15,
	name : '?智能模式',
	image : IMAGE_DIR + 'unknown.png'
} ];
var PRINCIPLE = {
	offset : [ 23, 68 ],
	data : [ [ 1, 1, 1, 1 ], [ 1, 0, 1, 0 ], [ 0, 1, 0, 1 ], [ 0, 1, 1, 0 ], [ 0, 0, 1, 1 ], [ 1, 0, 0, 1 ], [ 1, 1, 0, 0 ], [ 1, 1, 1, 0 ], [ 1, 1, 0, 1 ], [ 0, 1, 1, 1 ], [ 1, 0, 1, 1 ],
			[ 0, 1, 0, 0 ], [ 0, 0, 1, 0 ], [ 0, 0, 0, 1 ], [ 1, 0, 0, 0 ], [ 0, 0, 0, 0 ] ]
};
var MenuController = Class.create();
MenuController.prototype = {
	initialize : function(menu, options) {
		MENU = this;
		this.tiles = options.tiles;
		this.principle = options.principle;
		this.model = options.model;
		menu.html("");
		switch (this.model) {
		case 0:
			break;
		default:
			// 添加tiles选择
			this.m_tiles = $("<p>Tiles:<select id='tile' title='选择地图块' value='" + (this.tiles.length - 1) + "'></select></p>");
			for ( var i in this.tiles) {
				var cur = this.tiles[i];
				if (i == this.tiles.length - 1)
					this.m_tiles.find("#tile").append("<option value='" + cur.type + "' selected='selected'>" + cur.name + "</option>");
				else
					this.m_tiles.find("#tile").append("<option value='" + cur.type + "'>" + cur.name + "</option>");
			}
			this.m_tile_image = $("<p><img id='tile_image' title='当前地图块图片' style='margin-left:20px;' src='" + this.tiles[this.tiles.length - 1].image + "'/></p>");
			this.cur_tile_type = this.tiles.length - 1;
			menu.append(this.m_tiles);
			menu.append(this.m_tile_image);
			// 添加道路设置
			this.roads = new ArrayList();
			this.m_roads = $("<p><select id='roadlist' value='-1' title='选择道路'><option value='-1'>--NONE--</option></select><button id='addroad' title='add road'>+</button><button title='remove road' id='removeroad'>-</button></p>");
			menu.append(this.m_roads);
			var m_submit_all = $("<p><button id='submit_all' style='margin-left:5px;'>提交所有信息</button><button id='detectError' title='探测错误'>?</button></p>");
			menu.append(m_submit_all);
			break;
		}
		this.handleEvents();
	},
	// 根据Roads中数据生成option字符串
	roadListOptionsString : function(selected) {
		if (typeof (selected) == "undefined") {
			selected = -1;
		}
		var str = new Object();
		str.opts = "";
		var basic = selected == -1 ? "<option value='-1' selected='selected'>--NONE--</option>" : "<option value='-1'>--NONE--</option>";
		if (MENU.roads.size() == 0) {
			return basic;
		} else {
			MENU.roads.foreach(function(index, data, str) {
				if (data == null)
					return;
				if (index == selected) {
					str.opts += "<option value='" + data.id + "' selected='selected'>" + data.name + "</option>";
				} else {
					str.opts += "<option value='" + data.id + "'>" + data.name + "</option>";
				}
			}, str);
			return basic + str.opts;
		}
	},
	reviseRoads : function(roads) {
		roads.foreach(function(index, data) {
			data.id = index;
		});
	},
	handleEvents : function() {
		$("#tile").change(function() {
			var cur = $(this);
			var cur_tile = MENU.tiles[cur.val()];
			var ti = $("#tile_image");
			ti.attr("src", cur_tile.image);
			MENU.cur_tile_type = cur_tile.type;
		});
		$("#roadlist").change(function() {
			MAP.showRoadTips();
			var index = $(this).val();
			if (index == -1) {
				$("#road_name").val("");
				return;
			}
			$("#road_name").val(MENU.roads.get(index).name);
		});
		$("#addroad").click(function() {
			var id = MENU.roads.size();
			var ROAD_NAME = "道路" + id;
			$("#road_name").parent().remove();
			$(this).parent().after("<p><input id='road_name' style='width:105px;' value='" + ROAD_NAME + "'/><button id='modify_name'>√</button></p>");
			var road = new Road({
				id : id,
				name : ROAD_NAME
			});
			MENU.roads.insertElementAt(id, road);
			$("#modify_name").click(function() {
				var id = parseInt($("#roadlist").val());
				var cur = MENU.roads.get(id);
				cur.name = $("#road_name").val();
				$("#roadlist").html(MENU.roadListOptionsString(id));
			});
			$("#roadlist").html(MENU.roadListOptionsString(id));
			MAP.showRoadTips();
		});
		$("#removeroad").click(function() {
			var id = parseInt($("#roadlist").val());
			if (id == -1)
				return;
			MENU.roads.remove(id);
			MENU.reviseRoads(MENU.roads);
			$("#roadlist").html(MENU.roadListOptionsString());
			$("#road_name").val(MENU.roads.get(parseInt($("#roadlist").val())));
			MAP.showRoadTips();
		});
		$("#submit_all").click(function() {
			MAP.gatherRoadsInfo();
			if (LIGHTS.size() == 0 || ROADS.size() == 0)
				return;
			var json = [];
			ROADS.foreach(function(i, d, j) {
				var jj = d.toJSON();
				j[i] = jj;
			}, json);
			if (confirm("确认提交？"))
				ajax_post(HOST + "upload_points_info", "roads=" + JSON.stringify(json), function(msg) {
					MENU.postLightsInfo();
				});
		});
		$("#detectError").click(function() {
			if (typeof (drawed) == 'undefined' || !drawed) {
				MAP.gatherRoadsInfo();
				MENU.drawConfirmRoads();
				drawed = true;
			} else {
				drawed = false;
				if (typeof (drawed) != 'undefined')
					$("#canvas").remove();
			}

		});
	},
	drawConfirmRoads : function() {
		$("body").append("<canvas id='canvas' width='1800px' height='1800px' style='top:0px;left:0px;position:absolute;z-index=719;'></canvas>");
		var tool = new CanvasUtils({
			withposition : true
		});
		ROADS.foreach(function(index, data, tl) {
			var ps = [];
			data.locs.foreach(function(i, d) {
				/*
				 * d.x += MAP.parentOffset.left; d.y += MAP.parentOffset.top;
				 */ps[i] = d;
			}, ps);
			tool.drawLine(ps);
		}, tool);
	},
	postLightsInfo : function() {
		var json = [];
		LIGHTS.foreach(function(i, d, j) {
			var jj = d.toJSON();
			j[i] = jj;
		}, json);
		ajax_post(HOST + "upload_lights", "lights=" + JSON.stringify(json), function(msg) {
			MENU.postMapInfo();
		});
	},
	postMapInfo : function() {
		var json = [];
		var count = 0;
		for ( var i = 0; i < MAP.row; i++) {
			for ( var j = 0; j < MAP.col; j++) {
				var cur = MAP.data[i][j];
				if (typeof (cur) == "undefined" || cur.index != 0)
					continue;
				json[count++] = {
					row : i,
					col : j,
					index : 0,
					tile_type : cur.tile_type
				};
				json[count++] = {
					row : i,
					col : j + 1,
					index : 1,
					tile_type : cur.tile_type
				};
				json[count++] = {
					row : i + 1,
					col : j + 1,
					index : 2,
					tile_type : cur.tile_type
				};
				json[count++] = {
					row : i + 1,
					col : j,
					index : 3,
					tile_type : cur.tile_type
				};
			}
		}
		ajax_post(HOST + "upload_map", "tiles=" + JSON.stringify(json), function(msg) {
			alert("Upload Successfully");
		});
	}
};

var MapEntity = Class.create();
MapEntity.prototype = {
	initialize : function(options) {
		MAP = this;
		if (typeof (options) == 'undefined')
			return null;
		this.row = options.row;
		this.col = options.col;
		this.width = options.width;
		this.height = options.height;
		this.parent = options.parent;
		this.parentOffset = this.parent.offset();
		this.menu = options.menu;
		this.ctool = options.ctool;
		this.entryCtrl = options.entryCtrl;
		this.data = new Array(this.row);
		for ( var i = 0; i < this.row; i++) {
			this.data[i] = new Array(this.col);
		}
		switch (this.menu.model) {
		case 0:
			MAP.loadTilesInfo();
			break;
		default:
			break;
		}
		this.drawBasicFrame();
		this.handleEvent();

	},
	loadAllPoints : function(){
		ajax_post(HOST + "get_all_points", "area=1", function(msg) {
			if (msg != null) {
				var json = {};
				json = eval("(" + msg + ")");
				for ( var i in json) {
					var cur = json[i];
				}
			}
		});
	},
	loadTilesInfo : function() {
		ajax_post(HOST + "get_map", "", function(msg) {
			if (msg != null) {
				var json = {};
				json = eval("(" + msg + ")");
				for ( var i in json) {
					var cur = json[i];
					if (cur.index == 0) {
						var cur_tile = MAP.menu.tiles[cur.tile_type];
						MAP.setTileValue(cur.row, cur.col, cur_tile);
					}
				}
			}
		});
	},
	drawBasicFrame : function() {
		for ( var r = 0; r < this.row; r++) {
			var node = null;
			for ( var c = 0; c < this.col; c++) {
				node = $("<div class='column' row=" + r + " col=" + c + " id='" + r + "rc" + c + "'></div>");
				this.parent.append(node);
			}
			this.parent.append("<div class='clear'></div>");
		}
	},
	// 当鼠标停留在某个index为1的位置是，鼠标变手掌
	tileTip : function() {
		$(".column_no_border").mouseover(function() {
			var cur = $(this);
			var row = cur.attr("row");
			var col = cur.attr("col");
			var cur_tile = MAP.data[row][col];
			if (!!cur_tile && cur_tile.index == 0) {
				cur.addClass("column_pointer");
			}
		});
		$(".column_no_border").mouseout(function() {
			var cur = $(this);
			var row = cur.attr("row");
			var col = cur.attr("col");
			var cur_tile = MAP.data[row][col];
			if (!!cur_tile && cur_tile.index == 0) {
				cur.removeClass("column_pointer");
			}
		});
	},
	removeRoadTips : function(cur_id) {
		var un = $(".road_tag").eq(0).attr("cur_road_id");
		if (typeof (un) != "undefined" && cur_id != un) {
			$(".road_tag").remove();
		}
	},
	showRoadTips : function(cur_id) {
		$(".road_tag").remove();
		if (typeof (cur_id) == "undefined")
			cur_id = MAP.menu.m_roads.find("select").val();
		if (cur_id == -1)
			return;
		var cur_road = MAP.menu.roads.get(cur_id);
		cur_road.locs.foreach(function(index, data, ci) {
			MAP.addRoadTip(data.y, data.x, ci);
		}, cur_id);

	},
	addRoadTip : function(cur_row, cur_col, cur_id) {
		// 添加道路标志
		var cp = MAP.curTilePosition(cur_row, cur_col);
		var top = cp.y + MAP.parent.offset().top + 23 - 8;
		var left = cp.x + MAP.parent.offset().left + 23 - 8;
		$("body").append($("<div class='road_tag'  cur_road_id='" + cur_id + "' style='top:" + top + "px;left:" + left + "px;'>√</div>"));
	},
	handleEvent : function() {
		if(MAP.menu.model==0){
			// 单击canvas时
			if(typeof LOCS == "undefined"){
				LOCS = new Array(); 
				finding = false;
			}
			$("#canvas").click(function(e) {
				if (finding) {
					return;
				}
				var l = document.body.scrollLeft;
				var t = document.body.scrollTop;
				var loc = new Location(e);
				loc.x = loc.x + l - $(this).offset().left;
				loc.y = loc.y + t - $(this).offset().top;
				if (LOCS.length == 2) {
					LOCS[0] = LOCS[1];
					LOCS[1] = loc;
					MAP.ctool.clearCanvas();
				} else {
					LOCS[LOCS.length] = loc;
				}
				MAP.ctool.drawCircle(LOCS);
				if (LOCS[0]) {
					$("#start").html(
							"起点:(" + LOCS[0].x + ","
									+ LOCS[0].y+ ")");
					$("#start").css("color", "white");
				} else {
					$("#start").html("请地图上单击选择起点");
				}
				if (LOCS[1]) {
					$("#end").html(
							"终点:(" + LOCS[1].x + ","
									+ LOCS[1].y + ")");
					$("#end").css("color", "red");
				} else {
					$("#end").html("请地图上单击选择终点");
				}
			});
			$("#route").click(function(){
				TOP = LEFT = 0;
				MAP.ctool.clearCanvas();
				MAP.ctool.drawCircle(LOCS);
				locs = {};
				if (LOCS.length != 2) {
					alert(LOCS.length == 0 ? "请在地图上选择起点与终点"
							: "请在地图上选择终点");
					return;
				}
				finding = true;
				$("#loading")
						.html(
								"<img src='css/images/loading.gif'/>正在寻路...");
				var x = LOCS[0].x;
				var y = LOCS[0].y;
				var x1 = LOCS[1].x;
				var y1 = LOCS[1].y;
				var json = {};
				var points = [ {
					id : 0,
					x : x,
					y : y,
					area : cur_area
				}, {
					id : 0,
					x : x1,
					y : y1,
					area : cur_area
				} ];
				json = points;
				entryCtrl.post_ajax_then_do("basic",
						"points=" + JSON.stringify(json),
						function(json) {
							MAP.ctool.clearCanvas();
							json = eval("(" + json + ")");
							$("#loading").html("");
							finding = false;
							var size = json.length;
							locs = new ArrayList();
							for ( var i = 0; i < size; i++) {
								var cur = json[i];
								var loc = new Location(cur.x,
										cur.y, cur.id);
								locs.add(loc);
								entryCtrl.addEntry(
										entryCtrl.OPTERATION.BASIC, loc,
										controller, 0);
							}
							locs.addElementAt(0, new Location(
									x, y));
							locs.addElementAt(locs.size(),
									new Location(x1, y1));
							MAP.ctool = new CanvasUtils({
								points : locs.data()
							});
							MAP.ctool.drawLine();
						});
			});
		}
		// 单击地图单元格时
		$(".column").click(function() {
			var cur = $(this);
			var cur_row = parseInt(cur.attr("row"));
			var cur_col = parseInt(cur.attr("col"));
			if (cur_row == MAP.row - 1 || cur_col == MAP.col - 1)
				return;
			// 当前是否处于道路选择模式下
			var cur_id = MAP.menu.m_roads.find("select").val();
			if (cur_id == -1) {
				if (typeof (MAP.data[cur_row][cur_col]) == 'undefined') {
					MAP.drawTile(cur_row, cur_col);
				} else if (MAP.data[cur_row][cur_col].index == 0) {
					MAP.removeTile(cur_row, cur_col);
				}
			} else {
				// 选路模式
				var cur_road = MAP.menu.roads.get(cur_id);
				var cur_data = MAP.data[cur_row][cur_col];
				if (cur_road != null && typeof (cur_road) != "undefined" && typeof (cur_data) != "undefined" && cur_data.index == 0) {
					var addit = new Object();
					addit.add = true;
					cur_road.locs.foreach(function(index, data, addnow) {
						if (data.x == cur_col && data.y == cur_row) {
							addnow.add = false;
						}
					}, addit);
					if (addit.add) {
						var loc = new Location(cur_col, cur_row, cur_road.locs.size(), cur_area);
						cur_road.locs.add(loc);
						MAP.showRoadTips(cur_id);
					}
				}
			}
		});
	},
	northTile : function(cur_row, cur_col, count) {
		if (typeof (count) == 'undefined')
			count = 1;
		if (cur_row - count * 2 < 0)
			return undefined;
		var t = MAP.data[cur_row - count * 2][cur_col];
		if (!!t && t.index == 0)
			return t;
		return undefined;
	},
	southTile : function(cur_row, cur_col, count) {
		if (typeof (count) == 'undefined')
			count = 1;
		if (cur_row + count * 2 > MAP.height)
			return undefined;
		var t = MAP.data[cur_row + count * 2][cur_col];
		if (!!t && t.index == 0)
			return t;
		return undefined;
	},
	eastTile : function(cur_row, cur_col, count) {
		if (typeof (count) == 'undefined')
			count = 1;
		if (cur_col + count * 2 > MAP.width)
			return undefined;
		var t = MAP.data[cur_row][cur_col + count * 2];
		if (!!t && t.index == 0)
			return t;
		return undefined;
	},
	westTile : function(cur_row, cur_col, count) {
		if (typeof (count) == 'undefined')
			count = 1;
		if (cur_col - count * 2 < 0)
			return undefined;
		var t = MAP.data[cur_row][cur_col - count * 2];
		if (!!t && t.index == 0)
			return t;
		return undefined;
	},
	principleSum : function(n, e, s, w) {
		return n * 1000 + e * 100 + s * 10 + w;
	},
	seachTile : function(n, e, s, w) {
		var prns = MAP.menu.principle.data;
		var tsum = MAP.principleSum(n, e, s, w);
		for (i in prns) {
			var cur = prns[i];
			if (tsum == MAP.principleSum(cur[0], cur[1], cur[2], cur[3]))
				return i;
		}
	},
	crossSeach : function(cur_row, cur_col, deep, remove) {
		var cur = MAP.data[cur_row][cur_col];
		if (!remove && !deep && !cur)
			return;
		var n = !!MAP.northTile(cur_row, cur_col) ? 1 : 0;
		var e = !!MAP.eastTile(cur_row, cur_col) ? 1 : 0;
		var s = !!MAP.southTile(cur_row, cur_col) ? 1 : 0;
		var w = !!MAP.westTile(cur_row, cur_col) ? 1 : 0;
		var cur_tile = MAP.menu.tiles[MAP.seachTile(n, e, s, w)];
		if (typeof (remove) == "undefined")
			MAP.setTileValue(cur_row, cur_col, cur_tile);
		if (deep) {
			if (n == 1)
				MAP.crossSeach(cur_row - 2, cur_col, false);
			if (e == 1)
				MAP.crossSeach(cur_row, cur_col + 2, false);
			if (s == 1)
				MAP.crossSeach(cur_row + 2, cur_col, false);
			if (w == 1)
				MAP.crossSeach(cur_row, cur_col - 2, false);
		}
		return;
	},
	setTileValue : function(cur_row, cur_col, cur_tile) {
		MAP.data[cur_row][cur_col] = {
			tile_type : cur_tile.type,
			index : 0,
			col : cur_col,
			row : cur_row
		};
		MAP.data[cur_row][cur_col + 1] = {
			tile_type : cur_tile.type,
			index : 1,
			col : cur_col,
			row : cur_row
		};
		MAP.data[cur_row + 1][cur_col + 1] = {
			tile_type : cur_tile.type,
			index : 2,
			col : cur_col,
			row : cur_row
		};
		MAP.data[cur_row + 1][cur_col] = {
			tile_type : cur_tile.type,
			index : 3,
			col : cur_col,
			row : cur_row
		};
		var slice1 = $("#" + cur_row + "rc" + cur_col);
		var slice2 = $("#" + cur_row + "rc" + (cur_col + 1));
		var slice3 = $("#" + (cur_row + 1) + "rc" + (cur_col + 1));
		var slice4 = $("#" + (cur_row + 1) + "rc" + cur_col);
		slice1.html("<img src='" + cur_tile.image + "' class='tile_slice_1'/>");
		slice2.html("<img src='" + cur_tile.image + "' class='tile_slice_2'/>");
		slice3.html("<img src='" + cur_tile.image + "' class='tile_slice_3'/>");
		slice4.html("<img src='" + cur_tile.image + "' class='tile_slice_4'/>");
		slice1.attr("class", "column_no_border");
		slice2.attr("class", "column_no_border");
		slice3.attr("class", "column_no_border");
		slice4.attr("class", "column_no_border");
		MAP.tileTip();
	},
	drawTile : function(cur_row, cur_col) {
		if (typeof (MAP.data[cur_row][cur_col + 1]) == 'undefined' && typeof (MAP.data[cur_row + 1][cur_col + 1]) == "undefined" && typeof (MAP.data[cur_row + 1][cur_col]) == "undefined") {
			if (MAP.menu.cur_tile_type == MAP.menu.tiles.length - 1) {
				MAP.crossSeach(cur_row, cur_col, true);
				return;
			}
			var cur_tile = MAP.menu.tiles[MAP.menu.cur_tile_type];
			MAP.setTileValue(cur_row, cur_col, cur_tile);
		}
	},
	removeTile : function(cur_row, cur_col) {
		MAP.data[cur_row][cur_col] = undefined;
		MAP.data[cur_row][cur_col + 1] = undefined;
		MAP.data[cur_row + 1][cur_col + 1] = undefined;
		MAP.data[cur_row + 1][cur_col] = undefined;
		var slice1 = $("#" + cur_row + "rc" + cur_col);
		var slice2 = $("#" + cur_row + "rc" + (cur_col + 1));
		var slice3 = $("#" + (cur_row + 1) + "rc" + (cur_col + 1));
		var slice4 = $("#" + (cur_row + 1) + "rc" + cur_col);
		slice1.html("");
		slice2.html("");
		slice3.html("");
		slice4.html("");
		slice1.attr("class", "column");
		slice2.attr("class", "column");
		slice3.attr("class", "column");
		slice4.attr("class", "column");
		if (MAP.menu.cur_tile_type == MAP.menu.tiles.length - 1) {
			MAP.crossSeach(cur_row, cur_col, true, true);
			return;
		}
	},
	// 求当前块的绝对位置（不包含wrapper的offset）
	curTilePosition : function(row, col) {
		return {
			x : col * MAP.width,
			y : row * MAP.height
		};
	},
	gatherPointsInfo : function() {
		for ( var i = 0; i < MAP.row; i++) {
			for ( var j = 0; j < MAP.col; j++) {
				var cur = MAP.data[i][j];
				if (!cur || cur.index != 0)
					continue;
				var curPosition = MAP.curTilePosition(i, j);
				var prn = MAP.menu.principle.data[cur.tile_type];
				var offset = MAP.menu.principle.offset;
				cur.points = new Array();
				var prn_sum = prn[0] + prn[1] + prn[2] + prn[3];
				if (prn[0]) {
					// 北方点
					cur.points[0] = new Location(curPosition.x + offset[0], curPosition.y, 0, cur_area);
					cur.points[1] = new Location(curPosition.x + offset[1], curPosition.y, 0, cur_area);
					if (prn_sum == 1) {
						cur.points[4] = new Location(curPosition.x + offset[1], curPosition.y + 2 * MAP.height, 0, cur_area);
						cur.points[5] = new Location(curPosition.x + offset[0], curPosition.y + 2 * MAP.height, 0, cur_area);
					}

				}
				if (prn[1]) {
					// 东方点
					cur.points[2] = new Location(curPosition.x + 2 * MAP.width, curPosition.y + offset[0], 0, cur_area);
					cur.points[3] = new Location(curPosition.x + 2 * MAP.width, curPosition.y + offset[1], 0, cur_area);
					if (prn_sum == 1) {
						cur.points[6] = new Location(curPosition.x, curPosition.y + offset[1], 0, cur_area);
						cur.points[7] = new Location(curPosition.x, curPosition.y + offset[0], 0, cur_area);
					}
				}
				if (prn[2]) {
					// 南方点
					cur.points[4] = new Location(curPosition.x + offset[1], curPosition.y + 2 * MAP.height, 0, cur_area);
					cur.points[5] = new Location(curPosition.x + offset[0], curPosition.y + 2 * MAP.height, 0, cur_area);
					if (prn_sum == 1) {
						cur.points[0] = new Location(curPosition.x + offset[0], curPosition.y, 0, cur_area);
						cur.points[1] = new Location(curPosition.x + offset[1], curPosition.y, 0, cur_area);
					}
				}
				if (prn[3]) {
					// 西方点
					cur.points[6] = new Location(curPosition.x, curPosition.y + offset[1], 0, cur_area);
					cur.points[7] = new Location(curPosition.x, curPosition.y + offset[0], 0, cur_area);
					if (prn_sum == 1) {
						cur.points[2] = new Location(curPosition.x + 2 * MAP.width, curPosition.y + offset[0], 0, cur_area);
						cur.points[3] = new Location(curPosition.x + 2 * MAP.width, curPosition.y + offset[1], 0, cur_area);
					}
				}
			}
		}
	},
	// (x,y)相对于(x1,y1)的方向
	tilesDirection : function(x, y, x1, y1) {
		if (x == x1 && y > y1)
			return 2;
		if (x == x1 && y < y1)
			return 0;
		if (y == y1 && x > x1)
			return 1;
		if (y == y1 && x < x1)
			return 3;
	},
	gatherRoadsInfo : function() {
		MAP.gatherPointsInfo();
		MAP.gatherLightsInfo();
		ROADS = new ArrayList();
		MAP.menu.roads.foreach(function(index, data) {
			var locs = data.locs;
			var name = data.name;
			var points = [ new ArrayList(), new ArrayList() ];
			locs.foreach(function(i, d, t) {
				var cur = MAP.data[d.y][d.x];
				var prn = MAP.menu.principle.data[cur.tile_type];
				// 当为开始tile时，提取两个方向的关键点
				if (i == 0) {
					var next = t.locs.get(i + 1);
					var dir = MAP.tilesDirection(next.x, next.y, d.x, d.y);
					var p1 = cur.points[(dir * 2 + 5) % 8];
					var p2 = cur.points[dir * 2];
					var p3 = cur.points[(dir * 2 + 4) % 8];
					var p4 = cur.points[dir * 2 + 1];
					if (typeof (p1) != "undefined" && p1 != null)
						t.points[0].add(p1);
					if (typeof (p2) != "undefined" && p1 != null)
						t.points[0].add(p2);
					if (typeof (p3) != "undefined" && p1 != null)
						t.points[1].add(p3);
					if (typeof (p4) != "undefined" && p1 != null)
						t.points[1].add(p4);
				} else if (i == t.locs.size() - 1) {
					var before = t.locs.get(i - 1);
					var dir = MAP.tilesDirection(d.x, d.y, before.x, before.y);
					var p1 = cur.points[dir * 2];
					var p2 = cur.points[dir * 2 + 1];
					if (typeof (p1) == "undefined" || p1 == null) {
						return;
					}
					t.points[0].add(p1);
					t.points[1].add(p2);
				} else {
					var next = t.locs.get(i + 1);
					var before = t.locs.get(i - 1);
					var dir1 = MAP.tilesDirection(before.x, before.y, d.x, d.y);
					var dir2 = MAP.tilesDirection(next.x, next.y, d.x, d.y);
					var prn_sum = prn[0] + prn[1] + prn[2] + prn[3];
					var cp1 = cur.points[dir2 * 2];
					var cp2 = cur.points[dir2 * 2 + 1];
					if (prn_sum == 2 && prn[0] + prn[2] != 2 && prn[1] + prn[3] != 2) {
						var before1 = points[0].get(points[0].size() - 1);
						var before2 = points[1].get(points[1].size() - 1);
						if ((dir1 + 1) % 4 == dir2) {// 向左转
							if (prn[0] + prn[1] == 2) {
								t.points[0].add(new Location(before1.x, cp1.y));
								t.points[1].add(new Location(before2.x, cp2.y));
							} else if (prn[1] + prn[2] == 2) {
								t.points[0].add(new Location(cp1.x, before1.y));
								t.points[1].add(new Location(cp2.x, before2.y));
							} else if (prn[2] + prn[3] == 2) {
								t.points[0].add(new Location(before1.x, cp1.y));
								t.points[1].add(new Location(before2.x, cp2.y));
							} else if (prn[3] + prn[0] == 2) {
								t.points[0].add(new Location(cp1.x, before1.y));
								t.points[1].add(new Location(cp2.x, before2.y));
							}
						} else {// 向右转
							if (prn[0] + prn[3] == 2) {
								t.points[0].add(new Location(before1.x, cp1.y));
								t.points[1].add(new Location(before2.x, cp2.y));
							} else if (prn[1] + prn[0] == 2) {
								t.points[0].add(new Location(cp1.x, before1.y));
								t.points[1].add(new Location(cp2.x, before2.y));
							} else if (prn[2] + prn[1] == 2) {
								t.points[0].add(new Location(before1.x, cp1.y));
								t.points[1].add(new Location(before2.x, cp2.y));
							} else if (prn[3] + prn[2] == 2) {
								t.points[0].add(new Location(cp1.x, before1.y));
								t.points[1].add(new Location(cp2.x, before2.y));
							}
						}
					}
					t.points[0].add(cp1);
					t.points[1].add(cp2);
				}
			}, {
				locs : locs,
				points : points
			});
			// 交换第一条路顺序
			for ( var i = 0; i < Math.floor(points[0].size() / 2); i++) {
				var temp = points[0].get(i);
				points[0].setElementAt(i, points[0].get(points[0].size() - 1 - i));
				points[0].setElementAt(points[0].size() - 1 - i, temp);
			}
			ROADS.add(new Road({
				id : 0,
				name : name + 0,
				locs : points[0]
			}));
			ROADS.add(new Road({
				id : 0,
				name : name + 1,
				locs : points[1]
			}));
		});
	},
	gatherLightsInfo : function() {
		LIGHTS = new ArrayList();
		for ( var i = 0; i < MAP.row; i++) {
			for ( var j = 0; j < MAP.col; j++) {
				var cur = MAP.data[i][j];
				if (!cur || cur.index != 0)
					continue;
				var prn = MAP.menu.principle.data[cur.tile_type];
				var prn_sum = prn[0] + prn[1] + prn[2] + prn[3];
				if (prn_sum == 1) {
					cur.lights = new Light();
					cur.startorend = true;
					// 此处的道路起点服务器端产生Distance的关系与其他三岔路口和十字路口完全相反（一般构建连通性都是N1->N2,X1->X2，但是道路开始点之间的关系建立是反的:N2->N1,W2->W1）
					if (prn[0] == 1) {
						cur.lights.set(4, cur.points[5]);
						cur.lights.set(5, cur.points[4]);
					} else if (prn[1] == 1) {
						cur.lights.set(6, cur.points[7]);
						cur.lights.set(7, cur.points[6]);
					} else if (prn[2] == 1) {
						cur.lights.set(0, cur.points[1]);
						cur.lights.set(1, cur.points[0]);
					} else if (prn[3] == 1) {
						cur.lights.set(2, cur.points[3]);
						cur.lights.set(3, cur.points[2]);
					}
					LIGHTS.add(cur.lights);
				} else if (prn_sum == 4 || prn_sum == 3) {
					cur.lights = new Light();
					cur.center = true;
					cur.lights.set(0, cur.points[0]);
					cur.lights.set(1, cur.points[1]);
					cur.lights.set(2, cur.points[2]);
					cur.lights.set(3, cur.points[3]);
					cur.lights.set(4, cur.points[4]);
					cur.lights.set(5, cur.points[5]);
					cur.lights.set(6, cur.points[6]);
					cur.lights.set(7, cur.points[7]);
					LIGHTS.add(cur.lights);
				}
			}
		}
	},
};